/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.doctor.netHosptial.service;


import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.RestUtils;
import com.jims.master.modules.base.netHosptial.entity.NetHisSerial;
import com.jims.master.modules.base.netHosptial.service.NetHisSerialService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.doctor.netHosptial.dao.NetClinicMasterDao;
import com.jims.master.modules.doctor.netHosptial.entity.NetClinicMaster;
import com.jims.master.modules.doctor.netHosptial.entity.NetPatMaster;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 门诊主记录Service
 * @author zp
 * @version 2016-05-31
 */
@Service
@Transactional(readOnly = true)
public class NetClinicMasterService extends CrudService<NetClinicMasterDao, NetClinicMaster> {

    @Autowired
    private NetClinicMasterDao clinicMasterDao;

    @Autowired
    private MasterBaseinfoService baseInfoService;

    @Autowired
    private NetPatMasterService netPatMasterService;

    @Autowired
    private NetHisSerialService netHisSerialService;

    /**
     * 获取用户门诊信息
     * @param-patientId
     * @return
     */
    public List<NetClinicMaster> getPatient(NetClinicMaster clinicMaster){
        return clinicMasterDao.getPatient(clinicMaster);
    }

    /**
     * 获得挂号信息，没有则生成
     * @param （必选）patientId
     * @param （必选）VISIT_DATE
     * @param （可选）idNo
     * @param （可选）ClinicLabel
     * @param （可选）clinicType
     * @return NetClinicMaster
     */
    /*@Transactional(readOnly = false)
    public NetClinicMaster initialClinicMaster( NetClinicMaster clinicMaster){
        NetClinicMaster sysEntity = getClinicMasterForFree(clinicMaster);
        if(sysEntity!=null){
            sysEntity.setRemarks("1");
            save(sysEntity);
            return sysEntity;
        }else{
            //visit_no
            NetHisSerial netHisSerial = new NetHisSerial();
            netHisSerial.setType("1");
            String visitNo=netHisSerialService.getNextNoByType(netHisSerial);
            netHisSerialService.updateBytype(netHisSerial);
            clinicMaster.setVisitNo(visitNo);
            //CLINIC_LABEL , VISIT_TIME_DESC，
            // clinicLabel;号别 没有则取配置
            if(StringUtils.isEmpty(clinicMaster.getClinicLabel())) {
                clinicMaster.setClinicLabel(RestUtils.getConfig("hishaobie"));
            }
            if(StringUtils.isNotEmpty(clinicMaster.getVisitTimeDesc())){
                clinicMaster.setVisitTimeDesc("白天");
            }
            clinicMaster.setSerialNo(getMaxSerialNoOfDate(clinicMaster));
            //NAME , NAME_PHONETIC , SEX , AGE ,
            //idNo存在则从平台系统用户取患者信息，否则从患者信息表取
            if(StringUtils.isNotEmpty(clinicMaster.getIdNo())){
                MasterBaseinfo baseInfo = baseInfoService.findByCardNum(clinicMaster.getIdNo());
                if(baseInfo!=null){
                    clinicMaster.setName(baseInfo.getName());
                    clinicMaster.setSex(baseInfo.getSex());
                    clinicMaster.setNamePhonetic("");
                }
            }else{
                NetPatMaster netPatMaster =netPatMasterService.findIndexInfoByPatientId(clinicMaster.getPatientId());
                if(netPatMaster!=null){
                    clinicMaster.setName(netPatMaster.getName());
                }
            }
            //IDENTITY , CHARGE_TYPE
            clinicMaster.setIdentity("一般人员");
            clinicMaster.setChargeType("自费");
            //INSURANCE_TYPE , INSURANCE_NO , UNIT_IN_CONTRACT , CLINIC_TYPE
            clinicMaster.setInsuranceType(null);
            clinicMaster.setInsuranceNo(null);
            clinicMaster.setUnitInContract("");
            // clinicType;	号类 没有则取配置
            if(StringUtils.isEmpty(clinicMaster.getClinicType())){
                clinicMaster.setClinicType(RestUtils.getConfig("hishaolei"));
            }
            //FIRST_VISIT_INDICATOR
            clinicMaster.setFirstVisitIndicator("1");
            //SYMPTOM
            clinicMaster.setSymptom("");
            //VISIT_DEPT
            StaffDicts staffDicts = new StaffDicts();
            staffDicts.setIdNo(UserUtils.getBaseInfo().getIdCard());
            staffDicts = staffDictService.getStaffDictsByIdNo(staffDicts);
            //if(staffDicts!=null)
            //clinicMaster.setVisitDept(staffDicts.getDeptId());
            if(StringUtils.isEmpty(clinicMaster.getVisitDept())){
                clinicMaster.setVisitDept(RestUtils.getConfig("hiskeshi"));
            }
            //DOCTOR , MR_PROVIDEd_INDICATOR , REGISTRATION_STATUS
            clinicMaster.setDoctor(null);
            clinicMaster.setMrProvidedIndicator("0");
            clinicMaster.setRegistrationStatus("1");
            //REGISTERING_DATE
            clinicMaster.setRegisteringDate(new Date());
            //REGIST_FEE , CLINIC_FEE , OTHER_FEE
            clinicMaster.setRegistFee(Double.valueOf("0.00"));
            clinicMaster.setClinicFee(Double.valueOf("0.00"));
            clinicMaster.setOtherFee(Double.valueOf("0.00"));
            //OPERATOR
            clinicMaster.setOperator("");
            //clinic_charge
            clinicMaster.setClinicCharge(Double.valueOf("0.00"));
            //mode_code , card_name , card_no , pay_way , invoice_no
            clinicMaster.setModeCode("A");
            clinicMaster.setCardName("现金");
            clinicMaster.setCardNo(null);
            clinicMaster.setPayWay("现金");
            clinicMaster.setInvoiceNo("");
            //CLINIC_NO , print_operator
            String clinicNo="";
            clinicNo=DateUtils.formatDate(clinicMaster.getVisitDate(),"yyyyMMdd");
            clinicNo = clinicNo+clinicMaster.getVisitNo();
            clinicMaster.setClinicNo(clinicNo);
            clinicMaster.setPrintOperator("");
            clinicMaster.setRemarks("1");
            save(clinicMaster);
            return clinicMaster;
        }
    }*/

    /**
     * 获得挂号信息，没有则生成
     * @param （必选）patientId
     * @param （必选）VISIT_DATE
     * @param （可选）idNo
     * @param （可选）ClinicLabel
     * @param （可选）clinicType
     * @return NetClinicMaster
     */
    @Transactional(readOnly = false)
    public NetClinicMaster initialVillageClinicMaster( NetClinicMaster clinicMaster){
        //visit_no
        NetHisSerial netHisSerial = new NetHisSerial();
        netHisSerial.setType("1");
        String visitNo=netHisSerialService.getNextNoByType(netHisSerial);
        netHisSerialService.updateBytype(netHisSerial);
        clinicMaster.setVisitNo(visitNo);
        //todo
        //CLINIC_LABEL , VISIT_TIME_DESC，
        // clinicLabel;号别 没有则取配置
        /*if(StringUtils.isEmpty(clinicMaster.getClinicLabel())) {
            clinicMaster.setClinicLabel(RestUtils.getConfig("hishaobie"));
        }
        clinicMaster.setVisitTimeDesc("白天");*/
        clinicMaster.setSerialNo(getMaxSerialNoOfDate(clinicMaster));
        //NAME , NAME_PHONETIC , SEX , AGE ,
        //idNo存在则从平台系统用户取患者信息，否则从患者信息表取
        /*if(StringUtils.isNotEmpty(clinicMaster.getIdNo())){
            MasterBaseinfo baseInfo = baseInfoService.findByCardNum(clinicMaster.getIdNo());
            if(baseInfo!=null){
                clinicMaster.setName(baseInfo.getName());
                clinicMaster.setSex(baseInfo.getSex());
                clinicMaster.setNamePhonetic("");
            }
        }else{
            NetPatMaster netPatMaster =netPatMasterService.findIndexInfoByPatientId(clinicMaster.getPatientId());
            if(netPatMaster!=null){
                clinicMaster.setName(netPatMaster.getName());
            }
        }*/
        //IDENTITY , CHARGE_TYPE
        clinicMaster.setIdentity("一般人员");
        clinicMaster.setChargeType("自费");
        //INSURANCE_TYPE , INSURANCE_NO , UNIT_IN_CONTRACT , CLINIC_TYPE
        clinicMaster.setInsuranceType(null);
        clinicMaster.setInsuranceNo(null);
        clinicMaster.setUnitInContract("");
        // clinicType;	号类 没有则取配置
        if(StringUtils.isEmpty(clinicMaster.getClinicType())){
            clinicMaster.setClinicType(RestUtils.getConfig("hishaolei"));
        }
        //FIRST_VISIT_INDICATOR
        clinicMaster.setFirstVisitIndicator("1");
        //SYMPTOM
        clinicMaster.setSymptom("");
        //VISIT_DEPT
        //todo
        /*if(StringUtils.isEmpty(clinicMaster.getVisitDept())){
            clinicMaster.setVisitDept(RestUtils.getConfig("hiskeshi"));
        }*/
        //DOCTOR , MR_PROVIDEd_INDICATOR , REGISTRATION_STATUS
        clinicMaster.setDoctor(null);
        clinicMaster.setMrProvidedIndicator("0");
        clinicMaster.setRegistrationStatus("1");
        //REGISTERING_DATE
        clinicMaster.setRegisteringDate(new Date());
        //REGIST_FEE , CLINIC_FEE , OTHER_FEE
        clinicMaster.setRegistFee(Double.valueOf("0.00"));
        clinicMaster.setClinicFee(Double.valueOf("0.00"));
        clinicMaster.setOtherFee(Double.valueOf("0.00"));
        //OPERATOR
        clinicMaster.setOperator("");
        //clinic_charge
        clinicMaster.setClinicCharge(Double.valueOf("0.00"));
        //mode_code , card_name , card_no , pay_way , invoice_no
        clinicMaster.setModeCode("A");
        clinicMaster.setCardName("现金");
        clinicMaster.setCardNo(null);
        clinicMaster.setPayWay("现金");
        clinicMaster.setInvoiceNo("");
        //CLINIC_NO , print_operator
        String clinicNo="";
        clinicNo= DateUtils.formatDate(clinicMaster.getVisitDate(), "yyyyMMdd");
        clinicNo = clinicNo+clinicMaster.getVisitNo();
        clinicMaster.setClinicNo(clinicNo);
        clinicMaster.setPrintOperator("");
        clinicMaster.setRemarks("1");
        save(clinicMaster);
        return clinicMaster;
    }
    /**
     * 获得挂号信息
     * @param-patientId
     * @param-VISIT_DATE
     * @return NetClinicMaster
     */
    public NetClinicMaster getClinicMasterForFree(NetClinicMaster clinicMaster){
        return clinicMasterDao.getClinicMasterForFree(clinicMaster);
    }
    /**
     * 获得挂号信息
     * @param-patientId
     * @return
     */
    public String getMaxClinicMasterOfDate(NetClinicMaster clinicMaster){
        return clinicMasterDao.getMaxClinicMasterOfDate(clinicMaster);
    }

    /**
     * 获得患者未传送的挂号信息
     * @param-patientId
     * @return
     */
    public List<NetClinicMaster> getClinicMasterToSend(NetClinicMaster clinicMaster){
        return clinicMasterDao.getClinicMasterToSend(clinicMaster);
    };

    //改变发送标志
    @Transactional(readOnly = false)
    public Integer send(NetClinicMaster clinicMaster){
        return dao.send(clinicMaster);
    }

    /**
     * 获得挂号信息
     * @param appointNo
     * @return NetClinicMaster
     */
    public NetClinicMaster getClinicMasterByAppointNo(String appointNo){
        return dao.getClinicMasterByAppointNo(appointNo);
    }

    /**
     * 获得门诊号
     */
    public String getMaxClinicNo(){
        return clinicMasterDao.getMaxClinicNo();
    }

    /**
     * 获得挂号信息
     * @param-visitDate
     * @param-visitNo
     * @return NetClinicMaster
     */
    public NetClinicMaster getClinicByEntity(NetClinicMaster clinicMaster){
        return dao.getClinicByEntity(clinicMaster);
    }
    /**
     * 获得MaxSerialNo
     * @param-VISIT_DATE
     * @return
     */
    public String getMaxSerialNoOfDate(NetClinicMaster clinicMaster){
        return dao.getMaxSerialNoOfDate(clinicMaster);
    }

    //为卫生院初始化ClinicMaster
    @Transactional(readOnly = false)
    public NetClinicMaster initialClinicMasterForVillage(String patientId,String orgId){
        NetClinicMaster clinicMaster = new NetClinicMaster();
        clinicMaster.setPatientId(patientId);
        clinicMaster.setVisitDate(new Date());
        NetPatMaster netPatMaster =netPatMasterService.findIndexInfoByPatientId(clinicMaster.getPatientId());
        if(netPatMaster!=null){
            clinicMaster.setIdNo(netPatMaster.getIdNo());
            clinicMaster.setSex(netPatMaster.getSex());
            if(StringUtils.isNotEmpty(netPatMaster.getAge()))
                clinicMaster.setAge(netPatMaster.getAge());
        }
        clinicMaster.setOrgId(orgId);
        //1要与新web-his统一
        //2考虑到新设计不在生成相关表数据，那么需要的字段都要加在NetClinicMaster里面
        clinicMaster = initialVillageClinicMaster(clinicMaster);
        return clinicMaster;
    }
}